{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "suffering-saver",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/subdomain-visit-count\n",
    "\n",
    "\n",
    "Runtime: 32 ms, faster than 6.03% of C++ online submissions for Subdomain Visit Count.\n",
    "Memory Usage: 16.6 MB, less than 5.78% of C++ online submissions for Subdomain Visit Count.\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <map>\n",
    "#include <iostream>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    vector<string> Split(string s, string delimiter)\n",
    "    {\n",
    "        vector<string> results;\n",
    "        int start = 0;\n",
    "        int end = s.find(delimiter);\n",
    "        while (end != -1) {\n",
    "            results.push_back(s.substr(start, end - start));\n",
    "            start = end + delimiter.size();\n",
    "            end = s.find(delimiter, start);\n",
    "        }\n",
    "        results.push_back(s.substr(start, end - start));\n",
    "        return results;\n",
    "    }\n",
    "\n",
    "    string Join(vector<string> arr, string delimiter) {\n",
    "        string result;\n",
    "        for (auto s : arr) {\n",
    "            result += s + delimiter;\n",
    "        }\n",
    "        return result.substr(0, result.size()-delimiter.size());\n",
    "    }\n",
    "\n",
    "    vector<string> subdomainVisits(vector<string>& cpdomains) {\n",
    "        //7:07\n",
    "        map<string, int> counter;\n",
    "        for(string one : cpdomains) {\n",
    "            auto count_and_domain = Split(one, \" \");\n",
    "            int count = stoi(count_and_domain[0]);\n",
    "            string domain = count_and_domain[1];\n",
    "            auto sub_domains = Split(domain, \".\");\n",
    "            for (int i=0; i<sub_domains.size(); i++) {\n",
    "                auto sub_domain = vector<string>(sub_domains.begin() + i, sub_domains.end());\n",
    "                string the_key = Join(sub_domain, \".\");\n",
    "                if (counter.find(the_key) != counter.end()) {\n",
    "                    counter[the_key] += count;\n",
    "                } else {\n",
    "                    counter.insert(pair<string, int>{the_key, count});\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "        vector<string> results;\n",
    "        for (auto const& x : counter) {\n",
    "            string count = to_string(x.second);\n",
    "            string domain = x.first;\n",
    "            results.push_back(count + \" \" + domain);\n",
    "        }\n",
    "        return results;\n",
    "    }\n",
    "    //7:27\n",
    "    //debug until 7:51\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "answering-hayes",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
